<?php
namespace BoxEdu\Behaviors;
class IntBehavior extends \Think\Behavior
{
    //教育网站初始化
    public function run(&$param)
    {
        ignore_user_abort(true);
        set_time_limit(0);
        /********教育网站会员初始化***/
        $this->edu_user();
        /*******自动取消订单程序**/
        $this->CancelOrder();


    }

    /*******教育网站的会员****/
    public function edu_user()
    {
        $user_id = session("user_id");
        $InviteEarnPid = intval(S(session_id()));
        $PidData = BoxModel("edu_user")->where(array('user_id'=>$InviteEarnPid))->find();
        $agent_pid=0;
        if($PidData['is_agent']==1){
            $agent_pid=$InviteEarnPid;
        }else{
            $agent_pid=intval($PidData['agent_pid']);
        }
        if (!$user_id) {
            return;
        }
        $data = BoxModel("edu_user")->where("user_id='$user_id'")->find();
        if (!$data) {
            $row = array(
                'user_id' => $user_id,
                'agent_pid' => $agent_pid,
                'school_pid' => intval($InviteEarnPid),
            );
            session(session_id(), NULL);
            BoxModel("edu_user")->add($row);
        } else if ($data['is_agent'] == 0 && $data['school_pid'] == 0 && intval($InviteEarnPid)) {
            $row = array(
                // 'user_id' => $user_id,
                'agent_pid' => $agent_pid,
                'school_pid' => intval($InviteEarnPid),
            );
            session(session_id(), NULL);
            BoxModel("edu_user")->where(array('user_id' => $user_id))->save($row);
        }

    }

    function subject_to_id($subjectid, $user_id)
    {
        $subject_find = BoxModel("edu_subject")->where(array('subjectid' => $subjectid))->find();
        $r = Logic("BoxEdu", "Subject")->has_buy($subjectid, $user_id);
        if ($r['error'] == 0) {
            $type = 1;
        }
        if ($r['error'] == 2) {
            $type = 2;
        }
        $subjectid_data = array(
            'type' => $type,
            'subjectid' => $subjectid,
            'end_date' => $subject_find['end_date'],
        );
        return $subjectid_data;
    }


    /******自动取得订单***/
    public function CancelOrder()
    {
        $order_vailtime = dict("autoCancelOrder");
        $order_vailtime = intval($order_vailtime);
        $date = date("Y-m-d H:i:s", strtotime("-$order_vailtime hour"));
        $model = BoxModel("EduSubjectOrder");
        $data = $model->where("addtime<'$date' and status=1")->select();
        foreach ($data as $one) {
            $order_sn = $one['order_sn'];
            $model->cancel($order_sn);
        }
    }


}
